import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {NzModalService} from 'ng-zorro-antd/modal';
import {BakeProPlan} from '../../../../model/ProPlan';
import {ProduceplaneditComponent} from '../produceplanedit/produceplanedit.component';
import {AlertService} from "../../../../services/alert.service";
import {AdminService} from "../../../../services/admin.service";
import {SdckeditComponent} from "../sdckedit/sdckedit.component";

@Component({
  selector: 'app-produceplanlist',
  templateUrl: './produceplanlist.component.html',
  styleUrls: ['./produceplanlist.component.css']
})
export class ProduceplanlistComponent implements OnInit {
  pl: Array<BakeProPlan>;

  constructor(private modal: NzModalService, private viewContainerRef: ViewContainerRef,
              private adminService: AdminService,
              private alertService: AlertService) {
    this.pl = new Array<BakeProPlan>();
  }

  ngOnInit(): void {
    this.getProducts();
  }

  getProducts(){
    this.adminService.getProplans().then((data:any)=>{
      if((data.msg == "ok")){
        this.pl = data.list
      }else{
        this.alertService.showError("获取生产计划失败")
      }
    })
  }

  add() {
    const modalwindow = this.modal.create({
      nzTitle: '烘焙生产计划信息编辑',
      nzContent: ProduceplaneditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype:"add"
      },

      nzOnOk:()=>new Promise(resolve => {


        let temp=modalwindow.getContentComponent().p;
        if(!temp.jhrq){
          this.alertService.showWarning('计划日期必填');
          resolve(false)
        }else if(!temp.scpch){
          this.alertService.showWarning('生产批次号必填');
          resolve(false)
        }else if(!temp.scmc){
          this.alertService.showWarning('生产名称必填');
          resolve(false)
        }else if(!temp.jhscsl){
          this.alertService.showWarning('计划生产数量必填');
          resolve(false)
        }else if(!temp.jhsdyl){
          this.alertService.showWarning('计划生豆数量必填');
          resolve(false)
        }else if(!temp.jhbcsl){
          this.alertService.showWarning('计划包材数量必填');
          resolve(false)
        }
        else {
          modalwindow.getContentComponent().save()

          setTimeout(()=>{

            resolve(true);
            this.getProducts();
          }, 1000)
        }


      })
    })


  }

update(id:string){
  const modalwindow = this.modal.create({
    nzTitle: '烘焙生产计划信息编辑',
    nzContent: ProduceplaneditComponent,
    nzViewContainerRef: this.viewContainerRef,
    nzComponentParams: {
      optype:"update",
      id:id
    },

    nzOnOk:()=>new Promise(resolve => {


      let temp=modalwindow.getContentComponent().p;
      if(!temp.jhrq){
        this.alertService.showWarning('计划日期必填');
        resolve(false)
      }else if(!temp.scpch){
        this.alertService.showWarning('生产批次号必填');
        resolve(false)
      }else if(!temp.scmc){
        this.alertService.showWarning('生产名称必填');
        resolve(false)
      }else if(!temp.jhscsl){
        this.alertService.showWarning('计划生产数量必填');
        resolve(false)
      }else if(!temp.jhsdyl){
        this.alertService.showWarning('计划生豆数量必填');
        resolve(false)
      }else if(!temp.jhbcsl){
        this.alertService.showWarning('计划包材数量必填');
        resolve(false)
      }
      else {
        modalwindow.getContentComponent().save()

        setTimeout(()=>{

          resolve(true);
          this.getProducts();
        }, 1000)
      }


    })
  })
}

sdck(d:BakeProPlan){
  const modalwindow = this.modal.create({
    nzTitle: '生豆出库信息编辑',
    nzContent: SdckeditComponent,
    nzViewContainerRef: this.viewContainerRef,
    nzComponentParams: {
      // optype:"update",
      o:d
    },

    nzOnOk:()=>new Promise(resolve => {


      let temp=modalwindow.getContentComponent().p;
      if(!temp.pm){
        this.alertService.showWarning('品名必填');
        resolve(false)
      }
      else {
        modalwindow.getContentComponent().save()

        setTimeout(()=>{

          resolve(true);
          this.getProducts();
        }, 1000)
      }


    })
  })
}

bcck(d:BakeProPlan){
  const modalwindow = this.modal.create({
    nzTitle: '包材出库信息编辑',
    nzContent: SdckeditComponent,
    nzViewContainerRef: this.viewContainerRef,
    nzComponentParams: {
      optype:"bcck",
      o:d
    },

    nzOnOk:()=>new Promise(resolve => {


      let temp=modalwindow.getContentComponent().p;
      if(!temp.pm){
        this.alertService.showWarning('品名必选');
        resolve(false)
      }
      else {
        modalwindow.getContentComponent().save()

        setTimeout(()=>{

          resolve(true);
          this.getProducts();
        }, 1000)
      }


    })
  })
}


delete(id:string){
  this.adminService.deleteProplanByid(id).then((data:any)=>{
    if(data.msg == "ok"){
      this.alertService.showSuccess("删除生产计划成功")
      this.getProducts();
    }else{
      this.alertService.showError("删除生产计划失败")
    }
  })
}

}
